package com.jn.zhinongbao.adapter;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.supportingagriculturaltreasures.bean.FarmerCircle;
import com.example.supportingagriculturaltreasures.database.DatabaseHelper;

import java.io.File;

public class FarmerCircleDetailActivity extends AppCompatActivity {

    private TextView tvAuthorName, tvContent, tvLocation, tvTime;
    private TextView tvLikeCount, tvCommentCount;
    private ImageView ivImage, ivLike, ivComment, ivShare;
    
    private DatabaseHelper databaseHelper;
    private FarmerCircle currentCircle;
    private int circleId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_farmer_circle_detail);

        initViews();
        initData();
        loadDataFromIntent();
        setupClickListeners();
    }

    private void initViews() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("农友圈详情");
        }

        tvAuthorName = findViewById(R.id.tv_author_name);
        tvContent = findViewById(R.id.tv_content);
        tvLocation = findViewById(R.id.tv_location);
        tvTime = findViewById(R.id.tv_time);
        tvLikeCount = findViewById(R.id.tv_like_count);
        tvCommentCount = findViewById(R.id.tv_comment_count);
        ivImage = findViewById(R.id.iv_image);
        ivLike = findViewById(R.id.iv_like);
        ivComment = findViewById(R.id.iv_comment);
        ivShare = findViewById(R.id.iv_share);
    }

    private void initData() {
        databaseHelper = new DatabaseHelper(this);
    }

    private void loadDataFromIntent() {
        circleId = getIntent().getIntExtra("circle_id", -1);
        
        if (circleId != -1) {
            // 从数据库获取最新数据
            currentCircle = databaseHelper.getFarmerCircleById(circleId);
            if (currentCircle != null) {
                displayCircleData(currentCircle);
            } else {
                // 如果数据库中没有数据，使用Intent传递的数据
                loadDataFromIntentExtras();
            }
        } else {
            loadDataFromIntentExtras();
        }
    }

    private void loadDataFromIntentExtras() {
        String content = getIntent().getStringExtra("circle_content");
        String author = getIntent().getStringExtra("circle_author");
        String location = getIntent().getStringExtra("circle_location");
        String time = getIntent().getStringExtra("circle_time");
        String imageUrl = getIntent().getStringExtra("circle_image_url");
        int likeCount = getIntent().getIntExtra("circle_like_count", 0);
        int commentCount = getIntent().getIntExtra("circle_comment_count", 0);

        tvAuthorName.setText(author);
        tvContent.setText(content);
        tvLocation.setText(location);
        tvTime.setText(time);
        tvLikeCount.setText(String.valueOf(likeCount));
        tvCommentCount.setText(String.valueOf(commentCount));

        // 加载图片
        if (imageUrl != null && !imageUrl.isEmpty()) {
            ivImage.setVisibility(View.VISIBLE);
            loadImage(imageUrl);
        } else {
            ivImage.setVisibility(View.GONE);
        }

        // 创建临时对象用于操作
        currentCircle = new FarmerCircle();
        currentCircle.setId(circleId);
        currentCircle.setContent(content);
        currentCircle.setAuthorName(author);
        currentCircle.setLocation(location);
        currentCircle.setCreateTime(time);
        currentCircle.setImageUrl(imageUrl);
        currentCircle.setLikeCount(likeCount);
        currentCircle.setCommentCount(commentCount);
    }

    private void displayCircleData(FarmerCircle circle) {
        tvAuthorName.setText(circle.getAuthorName());
        tvContent.setText(circle.getContent());
        tvLocation.setText(circle.getLocation());
        tvTime.setText(circle.getCreateTime());
        tvLikeCount.setText(String.valueOf(circle.getLikeCount()));
        tvCommentCount.setText(String.valueOf(circle.getCommentCount()));

        // 设置点赞状态
        if (circle.isLiked()) {
            ivLike.setImageResource(android.R.drawable.btn_star_big_on);
        } else {
            ivLike.setImageResource(android.R.drawable.btn_star_big_off);
        }

        // 加载图片
        if (circle.getImageUrl() != null && !circle.getImageUrl().isEmpty()) {
            ivImage.setVisibility(View.VISIBLE);
            loadImage(circle.getImageUrl());
        } else {
            ivImage.setVisibility(View.GONE);
        }
    }

    private void loadImage(String imageUrl) {
        if (imageUrl != null && !imageUrl.isEmpty()) {
            File imageFile = new File(imageUrl);
            if (imageFile.exists()) {
                ivImage.setImageBitmap(BitmapFactory.decodeFile(imageUrl));
            } else {
                ivImage.setImageResource(android.R.drawable.ic_menu_gallery);
            }
        }
    }

    private void setupClickListeners() {
        ivLike.setOnClickListener(v -> {
            if (currentCircle != null && circleId != -1) {
                if (currentCircle.isLiked()) {
                    // 取消点赞
                    databaseHelper.unlikeFarmerCircle(circleId);
                    currentCircle.setLiked(false);
                    currentCircle.setLikeCount(currentCircle.getLikeCount() - 1);
                    ivLike.setImageResource(android.R.drawable.btn_star_big_off);
                    Toast.makeText(this, "已取消点赞", Toast.LENGTH_SHORT).show();
                } else {
                    // 点赞
                    databaseHelper.likeFarmerCircle(circleId);
                    currentCircle.setLiked(true);
                    currentCircle.setLikeCount(currentCircle.getLikeCount() + 1);
                    ivLike.setImageResource(android.R.drawable.btn_star_big_on);
                    Toast.makeText(this, "点赞成功", Toast.LENGTH_SHORT).show();
                }
                tvLikeCount.setText(String.valueOf(currentCircle.getLikeCount()));
            }
        });

        ivComment.setOnClickListener(v -> {
            Toast.makeText(this, "评论功能开发中...", Toast.LENGTH_SHORT).show();
        });

        ivShare.setOnClickListener(v -> {
            if (currentCircle != null) {
                android.content.Intent shareIntent = new android.content.Intent(android.content.Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "分享农友圈：" + currentCircle.getContent());
                startActivity(android.content.Intent.createChooser(shareIntent, "分享到"));
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 